package cn.tech.controller;


import cn.tech.alibaba.VerificationCode;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
public class DuanxinController {


    /*
    用户短信登入
     */
@RequestMapping("/login")
    public  String login(HttpServletRequest request){
    //获取验证码
    String mycode = request.getParameter("code");
    System.out.println(mycode);
    //从Seesion作用域中的验证码比较
    if (mycode.equals(request.getSession().getAttribute("code"))){

        return "suceess";
    }
    return "error";







}

/*
发送验证码
 */
@RequestMapping("/sendCode")
@ResponseBody
public String sendCode(HttpServletRequest request){
     System.out.println(request.getParameter("phone"));
     //生成的验证码 加入Session作业域中
    String vcode = VerificationCode.getPhonemsg(request.getParameter("phone"));
    request.getSession().setAttribute("code",vcode);
    Map map=new HashMap();
    if (vcode!=null) {
        map.put("type",1);

    }
    return JSON.toJSONString(map);

}
}
